package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanIcpIpRecord.LOAN_ICP_IP_RECORD;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.family.IcpWebIpVisitRecordRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.IcpWebIpVisitRecordSearchRequest;
import com.tuanzi.loan.vo.response.family.IcpWebIpVisitRecordSearchResponse;
import com.tuanzi.server.loan_base.util.DateUtils;

/**
 * ICP备案网站IP访问记录 JOOQRepositoryImplement
 * @author JinRuiyang 2017/12/14 10:20:08
 */
@Repository
public class IcpWebIpVisitRecordRepositoryImpl extends JOOQBaseRepository implements IcpWebIpVisitRecordRepository {

    /**
     * IcpWebIpVisitRecord查询
     *
     * @param searchRequest 请求信息
     * @param pageable      分页信息
     * @return 响应列表
     */
    @Override
    public Page<IcpWebIpVisitRecordSearchResponse> list(IcpWebIpVisitRecordSearchRequest searchRequest, Pageable pageable) {

        fields(LOAN_ICP_IP_RECORD.fields());
        ge(LOAN_ICP_IP_RECORD.UPDATE_TIME,searchRequest.getStartTime());
        le(LOAN_ICP_IP_RECORD.UPDATE_TIME,searchRequest.getEndTime());
        orderBy(LOAN_ICP_IP_RECORD.UPDATE_TIME, Sort.Direction.DESC);
        return search(pageable, record -> {
           IcpWebIpVisitRecordSearchResponse response = new IcpWebIpVisitRecordSearchResponse();
           response.setId(record.get(LOAN_ICP_IP_RECORD.IP));
           response.setIp(record.get(LOAN_ICP_IP_RECORD.IP));
           response.setFirstVisitTime(DateUtils.formatDateToYyyyMMddHHmmss(record.get(LOAN_ICP_IP_RECORD.CREATE_TIME)));
           response.setLastVisitTime(DateUtils.formatDateToYyyyMMddHHmmss(record.get(LOAN_ICP_IP_RECORD.UPDATE_TIME)));
           return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_ICP_IP_RECORD;
    }
}
